<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考生管理</el-breadcrumb-item>
      <el-breadcrumb-item>考生列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="examId" label="报考内容" width="200" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="applyName" label="考生姓名" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="idNumber" label="考生身份证号" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="examRoom" label="考场号" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="examNumber" label="准考证号" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="identificationPhoto" label="证件照" header-align="center" show-overflow-tooltip></el-table-column>


      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 以下是修改考生的弹出表单Dialog -->
    <el-dialog title="修改考生" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" style="width:500px ;margin: 0 auto">
        <el-form-item label="考生姓名" prop="applyName">
          <el-input v-model="editForm.applyName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="editForm.idNumber"></el-input>
        </el-form-item>
        <el-form-item label="证件照" prop="identificationPhoto">
          <el-input v-model="editForm.identificationPhoto"></el-input>
        </el-form-item>
        <el-form-item label="考试科目" :label-width="formLabelWidth">
          <el-input v-model="editForm.examId" ></el-input>
        </el-form-item>
        <el-form-item label="考试分数" :label-width="formLabelWidth">
          <el-input v-model="editForm.score" ></el-input>
        </el-form-item>
        <el-form-item label="考生状态" :label-width="formLabelWidth">
          <el-input v-model="editForm.status" ></el-input>
        </el-form-item>
        <el-form-item label="考场" :label-width="formLabelWidth">
          <el-input v-model="editForm.examRoom" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      // currentParentId: 0,
      // history: [],
      dialogFormVisible: false,
      editForm: {
        applyName: '',
        idNumber: '',
        identificationPhoto: '',
        status: '',
        examId:'',
        score: '',
        examRoom: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {


    showEditFormDialog(examUser) {
      this.editForm = examUser;
      this.dialogFormVisible = true;
      console.log(examUser)

      let url = 'http://120.77.154.166:8080/examUser/' + examUser.id+'/select';
      console.log('url = ' + url);

      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadExamUserList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://120.77.154.166:8080/examUser/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改考生成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadExamUserList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadExamUserList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    openDeleteConfirm(examUser) {
      let message = '此操作将永久删除【' + examUser.id + '】考生，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(examUser);
      }).catch(() => {
      });
    },
    handleDelete(examUser) {
      let url = 'http://120.77.154.166:8080/examUser/' + examUser.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' +examUser.name + '】考生成功！',
            type: 'success'
          });
          this.loadExamUserList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadExamUserList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    loadExamUserList() {
      let url = 'http://120.77.154.166:8080/examUser/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
          console.log(this.tableData)
        }
      });
    }
  },
  mounted() {
    this.loadExamUserList();
  }
}
</script>
